﻿using System;
using FairyGUI;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    [GameUIView(UIViewType.Login)]
    public class LoginView : GameUIView
    {
        public override string PackageName { get; set; } = "Battle";

        public override string ViewName { get; set; } = "Enter";

        public override void Init()
        {
            MainView.GetChild("n0").onClick.Add(()=>FGuiComponent.Single.LoadSence(UIViewType.Battle));
            MainView.GetChild("n1").onClick.Add(() =>
            {
                FGuiComponent.Single.LoadSence(UIViewType.DeckMake);
            });
            MainView.GetChild("n2").onClick.Add(() =>
            {
                FGuiComponent.Single.LoadSence(UIViewType.CardMake);
            });

            MainView.GetChild("n9").onClick.Add(() =>
            {
                Login(MainView.GetChild("n3").text, MainView.GetChild("n4").text);
            });
            MainView.GetChild("n10").onClick.Add(() =>
            {
                Resgister(MainView.GetChild("n3").text, MainView.GetChild("n4").text);
            });
            MainView.GetChild("n12").onClick.Add(() =>
            {
                CreatRoom(MainView.GetChild("n14").text);
            });
            MainView.GetChild("n11").onClick.Add(() =>
            {
                GetRoomList();
            });
            MainView.GetChild("n15").onClick.Add(() =>
            {
                JoinRoom(MainView.GetChild("n14").text);
            });
            MainView.GetChild("n18").onClick.Add(() =>
            {
                Send(MainView.GetChild("n17").text);
            });
        }
        public override void OnEnter()
        {

        }

        public void AddTalk(string message)
        {
            GComponent g= MainView.GetChild("n8").asList.AddItemFromPool().asCom;
            g.text = message;
        }

        void Send(string Message)
        {
            SessionComponent.Instance.Session.Send(new TalkRoom() { RoomName = JoinedRoom, MessageInfo = Message });
        }
        string JoinedRoom;
        async void JoinRoom(string RoomName)
        {
            M2C_Join_Room response = await SessionComponent.Instance.Session.Call<M2C_Join_Room>(new C2M_Join_Room() { RoomName = RoomName });
            if (response.Error == ErrorCode.ERR_JoinRoomError)
            {
                Log.Debug("加入房间失败");
                return;
            }
            JoinedRoom = RoomName;
            Log.Debug("加入房间成功");
        }

        async void CreatRoom(string RoomName)
        {
            M2C_CreatRoom response=await SessionComponent.Instance.Session.Call<M2C_CreatRoom>(new C2M_CreatRoom() { RoomName = RoomName });
            if (response.Error == ErrorCode.ERR_RoomExist)
            {
                Log.Debug("房间已存在");
                return;
            }
            JoinedRoom = RoomName;
            Log.Debug("创建房间成功");
        }

        async void GetRoomList()
        {
            M2C_GetRoomList response = await SessionComponent.Instance.Session.Call<M2C_GetRoomList>(new C2M_GetRoomList());
            foreach(var a in response.RoomList)
            {
                Log.Debug(a.RoomName);
            }
        }

        async void Resgister(string Account,string Password)
        {
            try
            {
                Session session = Game.Scene.GetComponent<NetOuterComponent>().Create("127.0.0.1:10002");
                Game.Scene.GetComponent<SessionComponent>().Session = session;
                /*R2C_Ping r2C_Ping = await session.Call<R2C_Ping>(new C2R_Ping());
                Log.Debug("Ping");*/
                R2C_Register r2C_Register = await session.Call<R2C_Register>(new C2R_Register() { Account = Account, Password = Password });
                if (r2C_Register.Error == ErrorCode.ERR_AccountOrPasswordError)
                {
                    Log.Debug("账户已存在");
                }
                else
                    Log.Debug("注册成功");
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }

        async void Login(string Account, string Password)
        {
            Session session = null;
            try
            {
                session = Game.Scene.GetComponent<NetOuterComponent>().Create("127.0.0.1:10002");
                Game.Scene.GetComponent<SessionComponent>().Session = session;
                R2C_Login r2CLogin = await session.Call<R2C_Login>(new C2R_Login() { Account = Account, Password = Password });
                if (r2CLogin.Error == ErrorCode.ERR_AccountOrPasswordError)
                {
                    Log.Debug("错误的账号密码");
                    return;
                }
                //Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
                //Session gateSession = Game.Scene.GetComponent<NetOuterComponent>().Create("39.106.135.113:10008");              
                Log.Info($"登录成功");
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }
    }
}
